#include "Scene.h"
#include "render_v1_typedef.h"

hxe::Scene::~Scene()
{
    root->drop();

    if (SkyBox::skybox != nullptr)
    {
        SkyBox::skybox->drop();
        SkyBox::skybox = nullptr;
    }
}


hxe::Scene::Scene()
{
    root = new GameObject("root");
    root->layer = layerEverything;
}

void hxe::Scene::addGameObject(GameObject* gameObject)
{
    gameObject->transform->setParent(root->transform);
}

void hxe::Scene::setLight(Light* light)
{
    this->light = light;
    addGameObject(light->gameObject);
}

hxe::Light* hxe::Scene::getLight() const
{
    return light;
}


void hxe::Scene::setSkyBox(SkyBox* skyBox)
{
    if (SkyBox::skybox != nullptr)
    {
        SkyBox::skybox->drop();
        SkyBox::skybox = nullptr;
    }
    SkyBox::skybox = skyBox;
}


hxe::SkyBox* hxe::Scene::getSkyBox() const
{
    return SkyBox::skybox;
}


hxe::Transform* hxe::Scene::getRoot() const
{
    return this->root->transform;
}

void hxe::Scene::update()
{
    root->update();
}

void hxe::Scene::draw()
{
    Camera::sortCamera();
    std::vector<Camera*>* allCameras = Camera::allCameras;
    for (unsigned int i = 0; i < allCameras->size(); i++)
    {
        Camera* camera = (*allCameras)[i];
        Camera::current = camera;
        camera->begin();
        root->draw();
    }
}
